package org.example.traing.controller;


import org.example.traing.VO.CategoryVO;
import org.example.traing.VO.ClassVO;
import org.example.traing.VO.JobStatusVO;
import org.example.traing.VO.JobVO;
import org.example.traing.entity.JobDetail;
import org.example.traing.entity.UserDetail;
import org.example.traing.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Job")
public class JobController {

    @Autowired
    JobService jobService;

    @GetMapping("/Classes")
    public ResponseEntity<Object> getClasses() {

        List<ClassVO> classVOList = jobService.getJobClass();
        Map<String, Object> response = new HashMap<>();
        if (classVOList!=null){

            response.put("msg", "success");
            response.put("data",classVOList);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/Categories")
    public ResponseEntity<Object> getCategory() {

        List<CategoryVO> categoryVOList = jobService.getJobCategory();
        Map<String, Object> response = new HashMap<>();
        if (categoryVOList!=null){

            response.put("msg", "success");
            response.put("data",categoryVOList);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/jobs/{category}")
    public ResponseEntity<Object> getJob(@PathVariable String category) {

        List<JobDetail> jobVOList = jobService.getJob(category);
        Map<String, Object> response = new HashMap<>();
        if (jobVOList!=null){

            response.put("msg", "success");
            response.put("data",jobVOList);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/details/{jobId}")
    public ResponseEntity<Object> getJobDetail(@PathVariable String jobId) {

        JobDetail jobDetail = jobService.getJobDetail(jobId);
        Map<String, Object> response = new HashMap<>();
        if (jobDetail!=null){

            response.put("msg", "success");
            response.put("data",jobDetail);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @GetMapping("/status/{jobId}")
    public ResponseEntity<Object> getJobStatus(@PathVariable String jobId) {

        List<JobStatusVO> jobStatusVOS = jobService.getJobStatus(jobId);
        Map<String, Object> response = new HashMap<>();
        if (jobStatusVOS!=null){

            response.put("msg", "success");
            response.put("data",jobStatusVOS);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }



    /*可以测试一下事务机制*/
    @PostMapping("/jobs/{categoryName}")
    public ResponseEntity<Object> addJob(@PathVariable String categoryName,@RequestBody JobDetail jobDetail) {

        int x = jobService.addJob(jobDetail,categoryName);
        Map<String, Object> response = new HashMap<>();
        if (x!=0){

            response.put("msg", "success");
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @GetMapping("/jobs")
    public ResponseEntity<Object> getAllJobs() {

        List<JobDetail> jobDetailList = jobService.getAllJobs();
        Map<String, Object> response = new HashMap<>();
        if (jobDetailList!=null){

            response.put("msg", "success");
            response.put("data",jobDetailList);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/categories/{categoryName}")
    public ResponseEntity<Object> getCategoryByClass( String categoryName) {

        List<CategoryVO> categoryVOList = jobService.getCategoryByClass(categoryName);
        Map<String, Object> response = new HashMap<>();
        if (categoryVOList!=null){

            response.put("msg", "success");
            response.put("data",categoryVOList);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @PutMapping("/jobs")
    public ResponseEntity<Object> getCategoryByClass(@RequestBody JobDetail jobDetail) {

        JobDetail jobDetail1 = jobService.updateJob(jobDetail);
        Map<String, Object> response = new HashMap<>();
        if (jobDetail1!=null){

            response.put("msg", "success");
            response.put("data",jobDetail1);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }


    @GetMapping("/keys{key}")
    public ResponseEntity<Object> getKeyJob(@PathVariable String key) {

        List<JobDetail> keyjobS = jobService.getKeyJobs(key);
        Map<String, Object> response = new HashMap<>();
        if (keyjobS!=null){

            response.put("msg", "success");
            response.put("data",keyjobS);
            return ResponseEntity.ok(response);
        }else{

            response.put("msg", "failed");
            return ResponseEntity.ok(response);
        }
    }



}
